package com.luo.gmall.sms.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.luo.gmall.sms.entity.SkuFullReductionEntity;
import com.luo.gmall.sms.entity.SkuLadderEntity;
import com.luo.gmall.sms.mapper.SkuFullReductionMapper;
import com.luo.gmall.sms.mapper.SkuLadderMapper;
import com.luo.gmall.sms.vo.ItemSaleVo;
import com.luo.gmall.sms.vo.SmsSaleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.PageParamVo;

import com.luo.gmall.sms.mapper.SkuBoundsMapper;
import com.luo.gmall.sms.entity.SkuBoundsEntity;
import com.luo.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Autowired
    private SkuFullReductionMapper skuFullReductionMapper;

    @Autowired
    private SkuLadderMapper skuLadderMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSmsSaleVo(SmsSaleVo smsSaleVo) {
        // 3.1. 积分优惠
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(smsSaleVo, skuBoundsEntity);
        List<Integer> work = smsSaleVo.getWork();
        // 利用二进制来存储work数据
        if (!CollectionUtils.isEmpty(work)) {
            skuBoundsEntity.setWork(8 * work.get(0) + 4 * work.get(1) + 2 * work.get(2) + work.get(3));
        }
        baseMapper.insert(skuBoundsEntity);

        // 3.2. 满减优惠
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(smsSaleVo, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(smsSaleVo.getFullAddOther());
        skuFullReductionMapper.insert(skuFullReductionEntity);
        // 3.3. 数量折扣
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(smsSaleVo, skuLadderEntity);
        skuLadderEntity.setAddOther(smsSaleVo.getLadderAddOther());
        skuLadderMapper.insert(skuLadderEntity);

    }

    @Override
    public List<ItemSaleVo> queryListSalesBySkuId(Long skuId) {
        List<ItemSaleVo> itemSaleVos = new ArrayList<>();
        // 送积分
        SkuBoundsEntity skuBoundsEntity = baseMapper.selectOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id", skuId));
        if (skuBoundsEntity != null) {
            ItemSaleVo skuBoundsItemSale = new ItemSaleVo();
            skuBoundsItemSale.setType("积分");
            skuBoundsItemSale.setDesc("送" + skuBoundsEntity.getGrowBounds() + "的成长积分," + skuBoundsEntity.getBuyBounds() + "的购物积分");
            itemSaleVos.add(skuBoundsItemSale);
        }
        // 满减
        SkuFullReductionEntity skuFullReductionEntity = skuFullReductionMapper.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id", skuId));
        if (skuFullReductionEntity != null) {
            ItemSaleVo skuFullReductionItemSaleVo = new ItemSaleVo();
            skuFullReductionItemSaleVo.setType("满减");
            skuFullReductionItemSaleVo.setDesc("满" + skuFullReductionEntity.getFullPrice() + "减" + skuFullReductionEntity.getReducePrice());
            itemSaleVos.add(skuFullReductionItemSaleVo);
        }
        // 数量折扣
        SkuLadderEntity skuLadderEntity = skuLadderMapper.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id", skuId));
        if (skuLadderEntity != null) {
            ItemSaleVo skuLadderItemSaleVo = new ItemSaleVo();
            skuLadderItemSaleVo.setType("折扣");
            skuLadderItemSaleVo.setDesc("满" + skuLadderEntity.getFullCount() + "件，打" + skuLadderEntity.getDiscount().divide(new BigDecimal(10)) + "折");
            itemSaleVos.add(skuLadderItemSaleVo);
        }
        return itemSaleVos;
    }

}